package com.yny.administrator.android1607_meishi.avtivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yny.administrator.android1607_meishi.R;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;

/**
 * 实现注册Activity
 */
public class RegieterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mPhone;
    private EditText mPsd;
    private EditText mCheck;
    private Button btnCheck;
    private Button btnRegister;
    private BmobUser mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_regieter);
        //获取控件
        initView();
        //设置监听
        btnCheck.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
        Bmob.initialize(this, "88136fb03a4803b0213be8903cc26b72");
        mUser = new BmobUser();
    }

    /**
     * 获取控件
     */
    private void initView() {
        mPhone = (EditText) findViewById(R.id._phone);
        mPsd = (EditText) findViewById(R.id._password);
        mCheck = (EditText) findViewById(R.id._check);
        btnCheck = (Button) findViewById(R.id.btn_check);
        btnRegister = (Button) findViewById(R.id.btn_register);
    }

    /**
     * @param view 实现接口方法
     */
    @Override
    public void onClick(View view) {
        final String phoneNum = mPhone.getText().toString().trim();
        final String psd = mPsd.getText().toString().trim();
        String checkNum = mCheck.getText().toString().trim();
        switch (view.getId()) {
            //获取验证码
            case R.id.btn_check:
                if (phoneNum.length() != 11) {
                    Toast.makeText(this, "请输入11位有效手机号码", Toast.LENGTH_SHORT).show();
                } else {
                    //进行获取验证码操作和倒计时1分钟操作
                    BmobSMS.requestSMSCode(this, phoneNum, "短信模板", new RequestSMSCodeListener() {
                        @Override
                        public void done(Integer integer, BmobException e) {
                            if (e == null) {
                                //发送成功时，让获取验证码按钮不可点击，且为灰色
                                btnCheck.setClickable(false);
                                btnCheck.setBackgroundColor(Color.GRAY);
                                Toast.makeText(RegieterActivity.this, "验证码发送成功，请尽快使用", Toast.LENGTH_SHORT).show();
                                new CountDownTimer(60000, 1000) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        btnCheck.setBackgroundResource(R.drawable.button);
                                        btnCheck.setText(millisUntilFinished / 1000 + "秒");
                                    }

                                    @Override
                                    public void onFinish() {
                                        btnCheck.setClickable(true);
                                        btnCheck.setBackgroundResource(R.drawable.button);
                                        btnCheck.setText("重新发送");
                                    }
                                }.start();
                            } else {
                                Toast.makeText(RegieterActivity.this, "验证码发送失败，请检查网络连接", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }

                break;
            //进行注册
            case R.id.btn_register:
                if (phoneNum.length() == 0 || psd.length() == 0 || phoneNum.length() != 11 || checkNum.length() == 0) {
                    Toast.makeText(this, "手机号或验证码输入不合法", Toast.LENGTH_SHORT).show();
                } else {
                    BmobSMS.verifySmsCode(this, phoneNum, checkNum, new VerifySMSCodeListener() {
                        @Override
                        public void done(BmobException e) {
                            if (e == null) {
                                mUser.setUsername(phoneNum);
                                mUser.setPassword(psd);
                                mUser.signUp(RegieterActivity.this, new SaveListener() {
                                    @Override
                                    public void onSuccess() {
                                        Toast.makeText(RegieterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(RegieterActivity.this, MainActivity.class);
                                        startActivity(intent);
                                    }

                                    @Override
                                    public void onFailure(int i, String s) {
                                        Toast.makeText(RegieterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            } else {
                                Toast.makeText(RegieterActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                break;
        }
    }
}
